<?php
	include_once("Database.inc");
	
class Region {
	
	public $id;
	public $name;
	
	/*
	 * Obtains the list of all Regions currently contained in the REGIONS table.
	 */
	public static function getAllRegions() {
		$regions = array();
		
		$query = $GLOBALS["DB"] -> doQuery("SELECT REGIONCODE, REGIONNAME FROM REGIONS ORDER BY REGIONCODE");
		
		while ($row = mssql_fetch_array($query)) {
			array_push($regions, Region::getRegionFromArray($row));
		}
		
		return $regions;
	}

	/*
	 * Obtains the region object with the specified code, or ID.
	 */ 
	public static function getRegion($id) {
		$query = $GLOBALS["DB"] -> doQuery("SELECT REGIONCODE, REGIONNAME FROM REGIONS WHERE REGIONCODE=$id");
		
		if ($row = mssql_fetch_array($query)) {
			return Region::getRegionFromArray($row);
		} else {
			return null;
		}
	} 	
	
	/*
	 * Creates a Region from an array record.
	 */
	protected static function getRegionFromArray($array) {
		$r = new Region();
		
		$r -> id = $array[0];
		$r -> name = $array[1];
		
		return $r;	
	} 
	
}